/**
 * 
 */
package com.xbang.attendance.web.action.warehouse.addproductdetail;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.stock.InventoryRecord;
import com.xbang.attendance.service.InventoryService;
import com.xbang.attendance.service.SystemService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.page.Result;
import com.xbang.core.struts.support.SearchActionSupport;

import enumeration.InventoryOperation;

/**
 * @author dev.zhang
 * 
 */
public class Search extends SearchActionSupport {

	private static final long serialVersionUID = 8792923545485296635L;

	private static final Log logger = LogFactory.getLog(Search.class);

	@Autowired
	private InventoryService inventoryService;
	@Autowired
	private SystemService systemService;
	
	
	private List<InventoryRecord> inventoryRecords = new ArrayList<InventoryRecord>();
	
	private Integer id;
	private Integer rid;
	@Override
	public void resetCustomerSearchCriteria() {
		// TODO Auto-generated method stub

	}
	@Action("/warehouse/addproductdetail/${id}/dosearch/")
	public String getInventoryRecordsByPurchaseOrder(){
		
		String hqlSentence = "FROM InventoryRecord inv WHERE (inv.lastInventoryOperation = '"+InventoryOperation.OUT +"' OR inv.lastInventoryOperation = '"+InventoryOperation.TRANSFERRED+"')";
		//if (searchComponent.getOrderParameters().isEmpty()){
		//	searchComponent.addSearchElement("groupBy.inv.purchaseOrder.id","");
			//SElECT DISTINCT inv.purchaseOrder.id 
		//}
		//inventoryRecords = systemService.searchForList(hqlSentence, new HashMap());
		if(id != null)
			searchComponent.addSearchElement("eq.inv.purchaseOrder.id", id);
		
		if(searchComponent.getOrderParameters().isEmpty())
			searchComponent.addSearchElement("orderBy.inv.product.id", "");
		//searchComponent.addSearchElement("groupBy.inv.purchaseOrder.id","");
		Result result = systemService.searchByPage(hqlSentence, searchComponent, page);

		inventoryRecords = result.getItems();
//		
//		for (InventoryRecord inventoryRecord : inventoryRecords) {
////			String hql = "FROM InventoryRecord inv WHERE inv.lastInventoryOperation = '"+InventoryOperation.OUT +"' OR inv.lastInventoryOperation = '"+InventoryOperation.TRANSFERRED+"'";
////			//SearchComponent search= new SearchComponent();
////			SearchComponent tmpSearchComponent = new SearchComponent();
////			tmpSearchComponent.getParameters().put("eqPurchaseOrderId", inventoryRecord.getPurchaseOrder().getId());
////			tmpSearchComponent.getWhereSentences().put("eq.inv.purchaseOrder.id", " AND (warehouse.code = :eqPurchaseOrderId)");
//			//search.addSearchElement("eq.inv.purchaseOrder.id",inventoryRecord.getPurchaseOrder().getId());
//			List<InventoryRecord> list= inventoryService.getInventoryRecordsByPurchaseOrder(inventoryRecord.getPurchaseOrder());
//			boolean flag= true;
//			for (InventoryRecord inventoryRecord2 : list) {
//				//System.out.println(inventoryRecord2.getInventoryOperationDislpaly());
//				//System.out.println(inventoryRecord2.getIsAddProduct());
//				if(!inventoryRecord2.getIsAddProduct()){
//					flag = false;
//				}
//			}
//			inventoryRecord.setIsAddProduct(flag);
//		}	
		if(!inventoryRecords.isEmpty()){
			rid = inventoryRecords.get(0).getId();
		}
		page = result.getPage();
		
		return "detail";
	}
	
	
	@Action("/warehouse/addproductdetail/${id}/search/")
	public String toSearch(){

		return "search";
	}


	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public void setInventoryRecords(List<InventoryRecord> inventoryRecords) {
		this.inventoryRecords = inventoryRecords;
	}
	public List<InventoryRecord> getInventoryRecords() {
		return inventoryRecords;
	}
	public Integer getRid() {
		return rid;
	}
	public void setRid(Integer rid) {
		this.rid = rid;
	}
	
}
